home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / EffectEdit / EffectDoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  10.6 KB  |  392 lines

  1. // EffectDoc.cpp : implementation of the CEffectDoc class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "EffectEdit.h"
  6.  
  7. #include "EffectDoc.h"
  8. #include "UIElements.h"
  9. #include "RenderView.h"
  10. #include "TextView.h"
  11. #include "ErrorsView.h"
  12. #include "OptionsView.h"
  13.  
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19.  
  20. const char* g_strCodeDefault = 
  21.     "// Sample effect file\r\n"
  22.     "\r\n"
  23.     "// Here is a texture parameter\r\n"
  24.     "texture tex0 < string name = \"tiger\\\\tiger.bmp\"; >;\r\n"
  25.     "\r\n"
  26.     "// Here is a vector parameter representing the direction\r\n"
  27.     "// vector from a light source.  The UIDirectional annotation\r\n"
  28.     "// tells EffectEdit to create a user interface element that can\r\n"
  29.     "// be used to interactively change the light direction.\r\n"
  30.     "float3 lightDir\r\n"
  31.     "<\r\n"
  32.     "    string UIDirectional = \"Light Direction\";\r\n"
  33.     "> = {0.577, -0.577, 0.577};\r\n"
  34.     "\r\n"
  35.     "string XFile = \"tiger\\\\tiger.x\";   // Model to load\r\n"
  36.     "string BIMG  = \"misc\\\\lake.bmp\";  // Background image\r\n"
  37.     "DWORD  BCLR = 0xff202080;  // Background color (if no image)\r\n"
  38.     "\r\n"
  39.     "technique tec0\r\n"
  40.     "{\r\n"
  41.     "    pass p0\r\n"
  42.     "    {\r\n"
  43.     "        // Set up reasonable material defaults\r\n"
  44.     "        MaterialAmbient = {1.0, 1.0, 1.0, 1.0}; \r\n"
  45.     "        MaterialDiffuse = {1.0, 1.0, 1.0, 1.0}; \r\n"
  46.     "        MaterialSpecular = {1.0, 1.0, 1.0, 1.0}; \r\n"
  47.     "        MaterialPower = 40.0f;\r\n"
  48.     "        \r\n"
  49.     "        // Set up one directional light\r\n"
  50.     "        LightType[0]      = DIRECTIONAL;\r\n"
  51.     "        LightDiffuse[0]   = {1.0, 1.0, 1.0, 1.0};\r\n"
  52.     "        LightSpecular[0]  = {1.0, 1.0, 1.0, 1.0}; \r\n"
  53.     "        LightAmbient[0]   = {0.1, 0.1, 0.1, 1.0};\r\n"
  54.     "        LightDirection[0] = <lightDir>; // Use the vector parameter defined above\r\n"
  55.     "        LightRange[0]     = 100000.0;\r\n"
  56.     "        \r\n"
  57.     "        // Turn lighting on and use light 0\r\n"
  58.     "        LightEnable[0] = True;\r\n"
  59.     "        Lighting = True;\r\n"
  60.     "        SpecularEnable = True;\r\n"
  61.     "        \r\n"
  62.     "        // Set up texture stage 0\r\n"
  63.     "        Texture[0] = <tex0>; // Use the texture parameter defined above\r\n"
  64.     "        ColorOp[0] = Modulate;\r\n"
  65.     "        ColorArg1[0] = Texture;\r\n"
  66.     "        ColorArg2[0] = Diffuse;\r\n"
  67.     "        AlphaOp[0] = Modulate;\r\n"
  68.     "        AlphaArg1[0] = Texture;\r\n"
  69.     "        AlphaArg2[0] = Diffuse;\r\n"
  70.     "        MinFilter[0] = Linear;\r\n"
  71.     "        MagFilter[0] = Linear;\r\n"
  72.     "        MipFilter[0] = Linear;\r\n"
  73.     "        \r\n"
  74.     "        // Disable texture stage 1\r\n"
  75.     "        ColorOp[1] = Disable;\r\n"
  76.     "        AlphaOp[1] = Disable;\r\n"
  77.     "    }\r\n"
  78.     "}\r\n"
  79.     "";
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. // CEffectDoc
  83.  
  84. IMPLEMENT_DYNCREATE(CEffectDoc, CDocument)
  85.  
  86. BEGIN_MESSAGE_MAP(CEffectDoc, CDocument)
  87.     //{{AFX_MSG_MAP(CEffectDoc)
  88.     ON_COMMAND(ID_EDIT_USEEXTERNALEDITOR, OnEditUseExternalEditor)
  89.     ON_UPDATE_COMMAND_UI(ID_EDIT_USEEXTERNALEDITOR, OnUpdateEditUseExternalEditor)
  90.     ON_COMMAND(ID_EDIT_USESHADEROPTIMIZATION, OnEditUseShaderOptimization)
  91.     ON_UPDATE_COMMAND_UI(ID_EDIT_USESHADEROPTIMIZATION, OnUpdateEditUseShaderOptimization)
  92.     //}}AFX_MSG_MAP
  93. END_MESSAGE_MAP()
  94.  
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CEffectDoc construction/destruction
  97.  
  98. CEffectDoc::CEffectDoc()
  99. {
  100.     m_bUsingExternalEditor = FALSE;
  101.     m_bFirstTime = TRUE;
  102.     m_strCode = g_strCodeDefault;
  103.     m_bNeedToCompile = true;
  104.     m_iLineSelected = -1;
  105.  
  106.     m_bUsingShaderOptimization = AfxGetApp()->GetProfileInt( TEXT("Settings"), TEXT("UseShaderOptimization"), TRUE );
  107. }
  108.  
  109. CEffectDoc::~CEffectDoc()
  110. {
  111. }
  112.  
  113. BOOL CEffectDoc::OnNewDocument()
  114. {
  115.     if (!CDocument::OnNewDocument())
  116.         return FALSE;
  117.  
  118.     if( m_bFirstTime )
  119.     {
  120.         m_strCode = g_strCodeDefault;
  121.         m_bFirstTime = FALSE;
  122.     }
  123.     else
  124.     {
  125.         m_strCode.Empty();
  126.     }
  127.     m_bNeedToCompile = true;
  128.     m_iLineSelected = -1;
  129.  
  130.     GetRenderView()->ResetCamera();
  131.  
  132.     return TRUE;
  133. }
  134.  
  135.  
  136.  
  137. /////////////////////////////////////////////////////////////////////////////
  138. // CEffectDoc serialization
  139.  
  140. void CEffectDoc::Serialize(CArchive& ar)
  141. {
  142.     // Note: file is always stored as ANSI text, not Unicode
  143.     if (ar.IsStoring())
  144.     {
  145.         CHAR ch[2] = { 0, 0 };
  146.         WCHAR wch[2] = { 0, 0 };
  147.         for( INT ich = 0; ich < m_strCode.GetLength(); ich++ )
  148.         {
  149.             wch[0] = m_strCode[ich];
  150.             WideCharToMultiByte( CP_ACP, 0, wch, 2, ch, 2, NULL, NULL );
  151.             ar.Write( ch, 1 );
  152.         }
  153.     }
  154.     else
  155.     {
  156.         m_strCode.Empty();
  157.  
  158.         BOOL bKeepTabs = AfxGetApp()->GetProfileInt( TEXT("Settings"), TEXT("Keep Tabs"), FALSE );
  159.         int numSpaces = AfxGetApp()->GetProfileInt( TEXT("Settings"), TEXT("Num Spaces"), 4 );
  160.  
  161.         CHAR ch[2] = { 0, 0 };
  162.         WCHAR wch[2] = { 0, 0 };
  163.  
  164.         while( ar.Read( ch, 1 ) )
  165.         {
  166.             MultiByteToWideChar( CP_ACP, 0, ch, 2, wch, 2 );
  167.             if( wch[0] == TEXT('\t') && !bKeepTabs )
  168.             {
  169.                 for( int iSpace = 0; iSpace < numSpaces; iSpace++ )
  170.                     m_strCode += TEXT(' ');
  171.             }
  172.             else
  173.             {
  174.                 m_strCode += wch[0];
  175.             }
  176.         }
  177.         m_bNeedToCompile = true;
  178.         GetRenderView()->RequestUIReset();
  179.     }
  180. }
  181.  
  182. /////////////////////////////////////////////////////////////////////////////
  183. // CEffectDoc diagnostics
  184.  
  185. #ifdef _DEBUG
  186. void CEffectDoc::AssertValid() const
  187. {
  188.     CDocument::AssertValid();
  189. }
  190.  
  191. void CEffectDoc::Dump(CDumpContext& dc) const
  192. {
  193.     CDocument::Dump(dc);
  194. }
  195. #endif //_DEBUG
  196.  
  197. /////////////////////////////////////////////////////////////////////////////
  198. // CEffectDoc commands
  199.  
  200. void CEffectDoc::SetCode(CString str)
  201. {
  202.     if( m_strCode != str )
  203.     {
  204.         SetModifiedFlag();
  205.         m_strCode = str;
  206.         m_bNeedToCompile = true;
  207.     }
  208. }
  209.  
  210. void CEffectDoc::Compile( bool bForceCompile )
  211. {
  212.     if( !bForceCompile && !m_bNeedToCompile )
  213.         return;
  214.  
  215.     CRenderView* pRenderView = GetRenderView();
  216.     CErrorsView* pErrorsView = GetErrorsView();
  217.     COptionsView* pOptionsView = GetOptionsView();
  218.     if( pRenderView != NULL )
  219.     {
  220.         int iTechnique;
  221.         BOOL bTryLater;
  222.         CString strCode;
  223.         if( m_bUsingExternalEditor )
  224.         {
  225.             strCode = GetPathName();
  226.         }
  227.         else
  228.         {
  229.             strCode = m_strCode;
  230.         }
  231.         pRenderView->CompileEffect( strCode, m_bUsingShaderOptimization, m_bUsingExternalEditor,
  232.             m_strErrors, m_techniqueNameList, &iTechnique, &bTryLater );
  233.  
  234.         if( !bTryLater )
  235.         {
  236.             m_bNeedToCompile = false;
  237.  
  238.             if( pErrorsView != NULL )
  239.                 pErrorsView->ParseErrors();
  240.  
  241.             if( pOptionsView != NULL )
  242.                 pOptionsView->SetTechniqueNameList( m_techniqueNameList, iTechnique );
  243.         }
  244.     }
  245. }
  246.  
  247. CString CEffectDoc::GetErrorString()
  248. {
  249.     return m_strErrors;
  250. }
  251.  
  252. void CEffectDoc::SelectLine(int iLine)
  253. {
  254.     m_iLineSelected = iLine;
  255.     UpdateAllViews( NULL );
  256.     m_iLineSelected = -1;
  257. }
  258.  
  259. int CEffectDoc::GetSelectedLine()
  260. {
  261.     return m_iLineSelected;
  262. }
  263.  
  264. CRenderView* CEffectDoc::GetRenderView()
  265. {
  266.    POSITION pos = GetFirstViewPosition();
  267.    while (pos != NULL)
  268.    {
  269.       CView* pView = GetNextView(pos);
  270.       if( pView->IsKindOf( RUNTIME_CLASS( CRenderView ) ) )
  271.       {
  272.           return (CRenderView*)pView;
  273.       }
  274.    }
  275.    return NULL;
  276. }
  277.  
  278. CTextView* CEffectDoc::GetTextView()
  279. {
  280.    POSITION pos = GetFirstViewPosition();
  281.    while (pos != NULL)
  282.    {
  283.       CView* pView = GetNextView(pos);
  284.       if( pView->IsKindOf( RUNTIME_CLASS( CTextView ) ) )
  285.       {
  286.           return (CTextView*)pView;
  287.       }
  288.    }
  289.    return NULL;
  290. }
  291.  
  292. COptionsView* CEffectDoc::GetOptionsView()
  293. {
  294.    POSITION pos = GetFirstViewPosition();
  295.    while (pos != NULL)
  296.    {
  297.       CView* pView = GetNextView(pos);
  298.       if( pView->IsKindOf( RUNTIME_CLASS( COptionsView ) ) )
  299.       {
  300.           return (COptionsView*)pView;
  301.       }
  302.    }
  303.    return NULL;
  304. }
  305.  
  306. CErrorsView* CEffectDoc::GetErrorsView()
  307. {
  308.    POSITION pos = GetFirstViewPosition();
  309.    while (pos != NULL)
  310.    {
  311.       CView* pView = GetNextView(pos);
  312.       if( pView->IsKindOf( RUNTIME_CLASS( CErrorsView ) ) )
  313.       {
  314.           return (CErrorsView*)pView;
  315.       }
  316.    }
  317.    return NULL;
  318. }
  319.  
  320.  
  321. BOOL CEffectDoc::GetShowStats()
  322. {
  323.     CRenderView* pRenderView = GetRenderView();
  324.     if( pRenderView != NULL )
  325.         return pRenderView->GetShowStats();
  326.     else
  327.         return FALSE;
  328. }
  329.  
  330.  
  331. void CEffectDoc::ShowStats( BOOL bShowStats )
  332. {
  333.     CRenderView* pRenderView = GetRenderView();
  334.     if( pRenderView != NULL )
  335.         pRenderView->ShowStats( bShowStats );
  336. }
  337.  
  338. void CEffectDoc::OnEditUseExternalEditor()
  339. {
  340.     if( !m_bUsingExternalEditor )
  341.     {
  342.         // Before going external, make sure current file is saved
  343.         if( IsModified() || GetPathName().IsEmpty() )
  344.         {
  345.             if( IDCANCEL == MessageBox( GetTextView()->GetSafeHwnd(), TEXT("You will need to save this effect to a file before changing to external editing mode.  Press OK to save."), TEXT("EffectEdit"), MB_OKCANCEL ) )
  346.                 return;
  347.             if( !DoFileSave() )
  348.                 return;
  349.         }
  350.     }
  351.     m_bUsingExternalEditor = !m_bUsingExternalEditor;
  352.     GetTextView()->SetExternalEditorMode( m_bUsingExternalEditor );
  353.     Compile(true);
  354. }
  355.  
  356. void CEffectDoc::OnUpdateEditUseExternalEditor(CCmdUI* pCmdUI) 
  357. {
  358.     pCmdUI->SetCheck( m_bUsingExternalEditor );
  359. }
  360.  
  361. BOOL CEffectDoc::GetLastModifiedTime( CTime* pTime )
  362. {
  363.     WIN32_FILE_ATTRIBUTE_DATA data;
  364.     if( GetPathName().IsEmpty() )
  365.         return FALSE;
  366.     if( !GetFileAttributesEx( GetPathName(), GetFileExInfoStandard, &data ) )
  367.         return FALSE;
  368.     *pTime = data.ftLastWriteTime;
  369.  
  370.     return TRUE;
  371. }
  372.  
  373. void CEffectDoc::ReloadFromFile()
  374. {
  375.     OnOpenDocument( GetPathName() );
  376.     Compile();
  377. }
  378.  
  379. void CEffectDoc::OnEditUseShaderOptimization()
  380. {
  381.     m_bUsingShaderOptimization = !m_bUsingShaderOptimization;
  382.     AfxGetApp()->WriteProfileInt( TEXT("Settings"), TEXT("UseShaderOptimization"), m_bUsingShaderOptimization );
  383.     m_bNeedToCompile = TRUE;
  384.     Compile();
  385. }
  386.  
  387. void CEffectDoc::OnUpdateEditUseShaderOptimization(CCmdUI* pCmdUI) 
  388. {
  389.     pCmdUI->SetCheck( m_bUsingShaderOptimization );
  390. }
  391.  
  392.